﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SIOsystem.Model.ReportDataSetTableAdapters;
using SIOsystem.Model.OrderDataSetTableAdapters;

namespace SIOsystem.Control
{
    class ReportClass
    {
        static public DataTable getInOrderList(DateTime startDate, DateTime endDate, string keyword)
        {
            try
            {
                using (QuaryInOrderTableAdapter db = new QuaryInOrderTableAdapter())
                {
                    DataTable dt=db.QuaryInOrders(startDate, endDate, "%" + keyword + "%");
                    if (dt!=null)
                    {
                        string[] colString = { 
                                                 "IOID", "编号"
                                               , "PName", "材料名" 
                                               , "TypeName", "规格" 
                                               , "InAmt", "数量" 
                                               , "Unit", "单位" 
                                               , "InPrice", "单价" 
                                               , "SSum", "金额" 
                                               , "Remark", "备注" 
                                               , "Serials", "单号" 
                                               , "InDate", "入库日期" 
                                               , "ISID", "入库编号" 
                                             };
                        for (int i = 0; i < colString.Length/2; i ++)
                        {
                            dt.Columns[colString[2 * i]].SetOrdinal(i);
                            dt.Columns[i].ColumnName = colString[2 * i + 1];
                        }
                        while (dt.Columns.Count > colString.Length / 2)
                        {
                            dt.Columns.RemoveAt(colString.Length / 2);
                        }
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getOutOrderList(DateTime startDate, DateTime endDate, string keyword,string OutName,long DeptID)
        {
            try
            {
                using (QuaryOutOrderTableAdapter db = new QuaryOutOrderTableAdapter())
                {
                    DataTable dt = null;
                    if (DeptID==-1)
                    {
                        dt = db.QuaryOutOrdersNoDept(startDate, endDate, "%" + OutName + "%", "%" + keyword + "%", "%" + keyword + "%");
                    } 
                    else
                    {
                        dt = db.QuaryOutOrders(startDate, endDate, "%" + OutName + "%", "%" + keyword + "%", "%" + keyword + "%", DeptID);
                    }
                    if (dt!=null)
                    {
                        string[] colString = { 
                                                 "OSID", "OSID"
                                               , "OOID", "编号"
                                               , "PName", "材料名" 
                                               , "TypeName", "规格" 
                                               , "OutAmt", "数量" 
                                               , "Unit", "单位" 
                                               , "InPrice", "单价" 
                                               , "SSum", "金额" 
                                               , "Remark", "备注" 
                                               , "ProjectName", "关联项目" 
                                               , "Serials", "单号" 
                                               , "OutDate", "出库日期" 
                                             };
                        for (int i = 0; i < colString.Length / 2; i++)
                        {
                            dt.Columns[colString[2 * i]].SetOrdinal(i);
                            dt.Columns[i].ColumnName = colString[2 * i + 1];
                        }
                        while (dt.Columns.Count > colString.Length / 2)
                        {
                            dt.Columns.RemoveAt(colString.Length / 2);
                        }
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getApplyOrderList(DateTime startDate, DateTime endDate, string keyword)
        {
            try
            {
                using (QuaryApplyOrderTableAdapter db = new QuaryApplyOrderTableAdapter())
                {
                    DataTable dt = db.QuaryApplyOrders(startDate, endDate);
                    if (dt!=null)
                    {
                        string[] colString = { 
                                                 "AOID", "编号"
                                               , "PName", "材料名" 
                                               , "TypeName", "规格" 
                                               , "AppAmt", "数量" 
                                               , "Unit", "单位" 
                                               , "Remark", "备注" 
                                               , "ProjectID", "项目号" 
                                               , "ProjectName", "项目名" 
                                               , "Serials", "单号" 
                                               , "AppDate", "申购日期" 
                                             };
                        for (int i = 0; i < colString.Length / 2; i++)
                        {
                            dt.Columns[colString[2 * i]].SetOrdinal(i);
                            dt.Columns[i].ColumnName = colString[2 * i + 1];
                        }
                        while (dt.Columns.Count > colString.Length / 2)
                        {
                            dt.Columns.RemoveAt(colString.Length / 2);
                        }
                    }
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getInOutDetail(DateTime startDate, DateTime endDate, long mainCID)
        {
            try
            {
                using (InOutDetailTableAdapter db = new InOutDetailTableAdapter())
                {
                    return db.GetInOutDetail(startDate, startDate, startDate, endDate, startDate, endDate
                                           , startDate, startDate, startDate, endDate, startDate, endDate
                                           , mainCID, mainCID);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getDeptDetail(DateTime startDate, DateTime endDate, long DID,bool isSumMode)
        {
            try
            {
                using (DeptDetailTableAdapter db = new DeptDetailTableAdapter())
                {
                    if (isSumMode)
                    {
                        if (DID == -1)
                        {
                            return db.GetProjectSumNoProject(startDate, endDate);
                        }
                        return db.GetProjectSum(startDate, endDate, DID);
                    } 
                    else
                    {
                        if (DID == -1)
                        {
                            return db.GetDeptDetailNoDept(startDate, endDate);
                        }
                        return db.GetDeptDetail(startDate, endDate, DID);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }


        static public bool PrintInOrder(long IOID)
        {
            try
            {
                DataTable dt = OrderClass.getInOrder(IOID);
                if (dt == null) { return false; }
                if (dt.Rows.Count == 0) { return false; }
                PrintHelper ph = new PrintHelper(dt);
                ph.SetPrint("工程材料入库单", 150, 2, 6, new string[] {   "入库单号：RK-" + dt.Rows[0]["单号"].ToString(), ""
                                                                        , "入库日期：" + ((DateTime)dt.Rows[0]["入库日期"]).ToString("yyyy/MM/dd"), "" }
                                                    , new string[] { "经办人：", "" });
                ph.Print();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool PrintOutOrder(long OOID)
        {
            try
            {
                DataTable dt = OrderClass.getOutOrder(OOID);
                if (dt == null) { return false; }
                if (dt.Rows.Count == 0) { return false; }
                PrintHelper ph = new PrintHelper(dt);
                ph.SetPrint("工程材料出库单", 150, 4, 6, new string[] {  "项目编号：" +dt.Rows[0]["项目编号"].ToString()
                                                                        , "出库单号：CK-" + dt.Rows[0]["单号"].ToString()
                                                                        , "项目名称："+dt.Rows[0]["关联项目"].ToString()
                                                                        , "出库日期：" + ((DateTime)dt.Rows[0]["出库日期"]).ToString("yyyy/MM/dd") }
                                                    , new string[] { "经办人：", "领料人：" });
                ph.Print();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool PrintApplyOrder(long AOID)
        {
            try
            {
                DataTable dt = OrderClass.getAppOrder(AOID);
                if (dt == null) { return false; }
                if (dt.Rows.Count == 0) { return false; }
                PrintHelper ph = new PrintHelper(dt);
                ph.SetPrint(dt.Rows[0]["项目编号"].ToString() + "工程材料申购单", 180
                                                  , 8, -1, new string[] {  "工程项目名称：" +dt.Rows[0]["项目名称"].ToString(),""
                                                                        , "工程项目地点：" + dt.Rows[0]["项目地点"].ToString(),""
                                                                        , "送货地址："+dt.Rows[0]["送货地址"].ToString(),""
                                                                        , "接收人/电话："+dt.Rows[0]["接收人电话"].ToString(),""
                                                                        , "费用结算对象："+dt.Rows[0]["费用结算对象"].ToString(),""
                                                                        }
                                                    , new string[] { "申购人：             "+"部门领导：              " 
                                                                    + DateTime.Now.ToString("yyyy年MM月dd日"),"" });
                ph.Print();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool PrintProjectSum(long PID)
        {
            try
            {
                DataTable dt = ReportClass.getDeptDetail(new DateTime(1900, 1, 1), new DateTime(2900, 1, 1), PID, true);
                if (dt == null) { return false; }
                if (dt.Rows.Count == 0) { return false; }
                string[] colString = { 
                                             "PID", "编号"
                                           , "PName", "材料名" 
                                           , "TypeName", "规格" 
                                           , "OutAmt", "数量" 
                                           , "Unit", "单位" 
                                           , "InPrice", "单价" 
                                           , "SSum", "金额" 
                                           , "Remark", "备注" 
                                           , "ProjectID", "项目编号" 
                                           , "ProjectName", "关联项目" 
                                         };
                for (int i = 0; i < colString.Length / 2; i++)
                {
                    dt.Columns[colString[2 * i]].SetOrdinal(i);
                    dt.Columns[i].ColumnName = colString[2 * i + 1];
                }
                while (dt.Columns.Count > colString.Length / 2 )
                {
                    dt.Columns.RemoveAt(colString.Length / 2 );
                }
                PrintHelper ph = new PrintHelper(dt);
                ph.SetPrint("工程领用材料汇总表", 150, 2, 6, new string[] {  "项目编号：" +dt.Rows[0]["项目编号"].ToString()
                                                                        , ""
                                                                        , "项目名称："+dt.Rows[0]["关联项目"].ToString()
                                                                        , "打印日期：" + DateTime.Now.ToString("yyyy-MM-dd") }
                                                    , new string[] { "", "" });
                ph.Print();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool PrintProjectDetail(long PID)
        {
            try
            {
                DataTable dt = ReportClass.getDeptDetail(new DateTime(1900, 1, 1), new DateTime(2900, 1, 1), PID, false);
                if (dt == null) { return false; }
                if (dt.Rows.Count == 0) { return false; }
                string[] colString = { 
                                             "PID", "编号"
                                           , "PName", "材料名" 
                                           , "TypeName", "规格" 
                                           , "OutAmt", "数量" 
                                           , "Unit", "单位" 
                                           , "InPrice", "单价" 
                                           , "SSum", "金额" 
                                           , "Remark", "备注" 
                                           , "ProjectID", "项目编号" 
                                           , "ProjectName", "关联项目" 
                                           , "Serials", "单号" 
                                           , "OutDate", "出库日期" 
                                         };
                for (int i = 0; i < colString.Length / 2; i++)
                {
                    dt.Columns[colString[2 * i]].SetOrdinal(i);
                    dt.Columns[i].ColumnName = colString[2 * i + 1];
                }
                while (dt.Columns.Count > colString.Length / 2)
                {
                    dt.Columns.RemoveAt(colString.Length / 2);
                }
                PrintHelper ph = new PrintHelper(dt);
                ph.SetPrint("工程领用材料明细表", 150, 4, 6, new string[] {  "项目编号：" +dt.Rows[0]["项目编号"].ToString()
                                                                        , ""
                                                                        , "项目名称："+dt.Rows[0]["关联项目"].ToString()
                                                                        , "打印日期：" + DateTime.Now.ToString("yyyy-MM-dd") }
                                                    , new string[] { "", "" });
                ph.SetPrint("单号", new string[] { "领用日期：", "出库日期", "出库单号：CK-", "单号" });
                ph.Print();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

    }
}
